﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.door
{
    public partial class door_function_list : Web.UI.ManagePage
    {
        Model.manager mgr;
        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string keywords = string.Empty;
        //&&1 已上架 2  已下架
        protected int isOnShelf;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            this.isOnShelf = DTRequest.GetQueryInt("isOnShelf");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                if (Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager == null)
                {
                    Response.Redirect("/admin/login.aspx");
                }
                ChkAdminLevel("door_function_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;

                RptBind(this.isOnShelf, " id>0 " + CombSqlTxt(this.keywords), " sort asc");
            }
        }


        #region 数据绑定
        private void RptBind(int _isOnShelf, string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            if (_isOnShelf >= 0)
            {
                ddlShelf.SelectedValue = _isOnShelf.ToString();
                _strWhere += " and status=" + _isOnShelf.ToString();
            }
            this.txtKeywords.Text = this.keywords;
            //图表或列表显示
            BLL.sf_door_function bll = new BLL.sf_door_function();
            this.rptList2.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList2.DataBind();
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("door_function_list.aspx", "isOnShelf={0}&keywords={1}&page={2}",
                _isOnShelf.ToString(), this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (name like  '%")
                      .Append(_keywords)
                      .Append("%' or remarks like '%")
                      .Append(_keywords)
                      .Append("%')");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回图文每页数量
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("door_function_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //设置操作
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            ChkAdminLevel("door_function_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
            this.RptBind(this.isOnShelf, "  id>0" + CombSqlTxt(this.keywords), "id asc");
        }

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("door_function_list.aspx", "isOnShelf={0}&keywords={1}",
                this.isOnShelf.ToString(), txtKeywords.Text));
        }

        //&&上架与下架处理条件筛选
        protected void ddlShelf_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("door_function_list.aspx", "isOnShelf={0}&keywords={1}",
                 ddlShelf.SelectedValue, this.keywords));
        }


        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("door_function_page_size", _pagesize.ToString(), 43200);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("door_function_list.aspx", "isOnShelf={0}&keywords={1}",
                 this.isOnShelf.ToString(), this.keywords));
        }


        //批量删除 done
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("door_function_list", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0; //成功数量
            int errorCount = 0; //失败数量
            BLL.sf_door_function bll = new BLL.sf_door_function();
            for (int i = 0; i < rptList2.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList2.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList2.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount++;
                    }
                    else
                    {
                        errorCount++;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "删除功能模块成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("door_function_list.aspx", "isOnShelf={0}&keywords={1}", this.isOnShelf.ToString(), this.keywords));
        }

        //&&上架
        protected void btnOnShelf_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("door_function_list", DTEnums.ActionEnum.OnShelf.ToString()); //检查权限
            int sucCount = 0; //成功数量
            int errorCount = 0; //失败数量
            BLL.sf_door_function bll = new BLL.sf_door_function();
            for (int i = 0; i < rptList2.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList2.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList2.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.UpdateField(id, " status=0 "))
                    {
                        sucCount++;
                    }
                    else
                    {
                        errorCount++;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "上架功能模块内容成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("上架功能模块成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("door_function_list.aspx", "isOnShelf={0}&keywords={1}", this.isOnShelf.ToString(), this.keywords));
        }
        //&&批量下架 done
        protected void btnOffShelf_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("door_index_img_list", DTEnums.ActionEnum.OffShelf.ToString()); //检查权限
            int sucCount = 0; //成功数量
            int errorCount = 0; //失败数量
            BLL.sf_door_function bll = new BLL.sf_door_function();
            for (int i = 0; i < rptList2.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList2.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList2.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.UpdateField(id, " status=1 "))
                    {
                        sucCount++;
                    }
                    else
                    {
                        errorCount++;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "下架功能模块成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("下架功能模块成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("door_function_list.aspx", "isOnShelf={0}&keywords={1}", this.isOnShelf.ToString(), this.keywords));
        }

    }
}